﻿using System;
using System.Web;
using System.IO;
using System.Configuration;

namespace Mammoo.Log
{
    public static class LogHelper
    {
        /// <summary>
        /// 写文件形式记录异常，需要在Web.config的appSettings添加IS_LOG和LOG_PATH
        /// IS_LOG值为1表示记日志，值为0不记日志
        /// LOG_PATH如<add key="LOG_PATH" value="D:\\Log\\"/>
        /// </summary>
        /// <param name="e">捕获的异常</param>
        /// <param name="userName">登录系统的用户</param>
        public static void WriteLog(Exception e, string userName)
        {
            string isLog = ConfigurationManager.AppSettings["IS_LOG"];
            if (string.IsNullOrEmpty(isLog) || isLog == "0")
            {
                return;
            }

            DateTime createTime = DateTime.Now;

            string HttpReferrer = string.Empty;
            if (HttpContext.Current.Request.UrlReferrer != null)
            {
                HttpReferrer = HttpContext.Current.Request.UrlReferrer.ToString();
            }

            string userAgent = string.Empty;
            if (HttpContext.Current.Request.UserAgent != null)
            {
                userAgent = HttpContext.Current.Request.UserAgent;
            }

            string ip = string.Empty;
            if (HttpContext.Current.Request.UserHostAddress != null)
            {
                ip = HttpContext.Current.Request.UserHostAddress;
            }

            string HttpVerb = string.Empty;
            try
            {
                if (HttpContext.Current.Request != null && HttpContext.Current.Request.RequestType != null)
                {
                    HttpVerb = HttpContext.Current.Request.RequestType;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }

            string HttpPathAndQuery = string.Empty;
            if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null && HttpContext.Current.Request.Url.PathAndQuery != null)
            {
                HttpPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            }

            string LogName = DateTime.Now.ToShortDateString() + ".txt";

            string FilePath = ConfigurationManager.AppSettings["LOG_PATH"].ToString() + LogName;

            if (!System.IO.File.Exists(FilePath))
            {
                using (StreamWriter sw = System.IO.File.CreateText(FilePath))
                {
                    sw.WriteLine("Fields           :Value");
                    sw.WriteLine();
                }
            }

            using (StreamWriter sw = System.IO.File.AppendText(FilePath))
            {
                sw.WriteLine("----------------------------------------------------------------------------------------------------");
                sw.WriteLine("DateTime         :" + createTime);
                sw.WriteLine("IPAddress        :" + ip);
                sw.WriteLine("HttpReferrer     :" + HttpReferrer);
                sw.WriteLine("HttpVerb         :" + HttpVerb);
                sw.WriteLine("HttpPathAndQuery :" + HttpPathAndQuery);
                sw.WriteLine("UserName         :" + userName);
                sw.WriteLine("UserAgent        :" + userAgent);
                sw.WriteLine("Source           :" + e.Source);
                sw.WriteLine("Message          :" + e.Message);
                sw.WriteLine("StackTrace       :" + e.StackTrace);
                sw.WriteLine();
            }
        }
    }
}
